/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package artid.com.arttogo.service;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;


import java.util.ArrayList;

import artid.com.arttogo.adapter.LeDeviceListAdapter;
import artid.com.arttogo.bean.iBeaconClass;
import artid.com.arttogo.utils.MySharedPreferences;
import artid.com.arttogo.utils.ScanningDeviceUtils;

/**
 * Activity for scanning and displaying available Bluetooth LE devices.
 */
public class DeviceScanActivity extends ListActivity {
	private final static String TAG = DeviceScanActivity.class.getSimpleName();
	private final static String UUID_KEY_DATA = "0000ffe1-0000-1000-8000-00805f9b34fb";
	private LeDeviceListAdapter mLeDeviceListAdapter;
	int i=0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ScanningDeviceUtils.notificationNum=1;
		// 注册当前广播
//		IntentFilter filter = new IntentFilter("com.DeviceScanActivity.receiver");
//		registerReceiver(broadcastReceiver, filter);
		//(放入C端的时候改为true)
		MySharedPreferences.getMySharedPreferences(this).setValue("isOpenBluetooth", "true");
		//Service  守护进程
		if("true".equals(MySharedPreferences.getMySharedPreferences(this).getValue("isOpenBluetooth", "false"))){
			Intent serviceOne = new Intent();
			serviceOne.setClass(DeviceScanActivity.this, BluetoothServiceOne.class);  
			startService(serviceOne);  
			Intent serviceTwo = new Intent();
			serviceTwo.setClass(DeviceScanActivity.this, BluetoothServiceTwo.class);  
			startService(serviceTwo);
		}
	}





	// 广播监听
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getBooleanExtra("BluetoothReceiver", false) == true) {
				iBeaconClass.iBeacon ibeacon = (iBeaconClass.iBeacon) intent.getSerializableExtra("Bluetooth_ibeacon");
				if(ibeacon!=null){
//					mLeDeviceListAdapter.addDevice(ibeacon);
//					mLeDeviceListAdapter.notifyDataSetChanged();
				}
			}
		}
	};


	/**
	 * 判断服务是否在运行
	 * @param mContext
	 * @param serviceName
	 * @return
	 */
	public static boolean isServiceWorked(Context mContext, String serviceName) {
		// TODO Auto-generated method stub
		ActivityManager myManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
		ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager.getRunningServices(Integer.MAX_VALUE);
		for (int i = 0; i < runningService.size(); i++) {  
			if (runningService.get(i).service.getClassName().toString().equals(serviceName)) {  
//				Log.d("log", "当前运行的服务名称："+runningService.get(i).service.getClassName().toString());
				return true;  
			}  
		}  
		return false;  
	}


}